$(document).ready(function() {

    //Handle Start Button
    $("#btnStart").click(function() {
        HandleStartButton();
    });
    
    //Handle Test Button
    $("#btnTest").click(function() {
        HandleTestButton();
    });

    //Handle Clear Button
    $("#btnClear").click(function() {
        HandleClearButton();
    });

    //Handle Stop Button
    $("#btnStop").click(function() {
        HandleStopButton();
    });

    ShowConfiguration(!CM_Function.StringToBool(localStorage["IS_START"]));
});

//Handle Start Button
function HandleStartButton()
{
    if (!ValidateData())
    {
        $(".error").first().focus();
        return;
    }

    localStorage["IS_START"] = CM_Function.BoolToString(true);
    ShowConfiguration(false);
}

function ValidateData()
{
    //clear error
    $(".input").removeClass("error");

    var messageArr = new Array();

    CheckRequire("TeamOneName", messageArr);
    CheckRequire("TeamTwoName", messageArr);
    CheckSite("A", messageArr);
    CheckSite("B", messageArr);

    ShowMessage(messageArr);
    return messageArr.length === 0;
}

function CheckSite(siteName, messageArr)
{
    CheckRequire(siteName + "Name", messageArr);
    CheckRequire(siteName + "ValueOne", messageArr);
    CheckRequire(siteName + "ValueTwo", messageArr);
}

function CheckRequire(itemKey, messageArr)
{
    var item = $("#txt" + itemKey);
    if (!$.trim(item.val()))
    {
        item.addClass("error");
        var label = $("#lbl" + itemKey).html();
        messageArr.push(CM_Function.StringFormat("{0} là bắt buộc nhập.", label));
    }
}

function ShowMessage(messageArr) {
    //Clear message
    var divMessage = $("#message");
    divMessage.html("");

    if (messageArr.length === 0)
        return;

    var message = "<ul>";
    for (var i = 0; i < messageArr.length; i++)
    {
        message += "<li>" + messageArr[i] + "</li>";
    }
    message += "</ul>";

    divMessage.html(message);
}
//Handle Clear Button
function HandleClearButton()
{
    //clear error
    $(".input").removeClass("error");
    
    //Clear message
    $("#message").html("");
    
    $(".input").val("");
    $("#txtTeamOneName").focus();
}

//Handle Stop Button
function HandleStopButton()
{
    localStorage["IS_START"] = CM_Function.BoolToString(false);
    ShowConfiguration(true);
}

function ShowConfiguration(value)
{
    if (value)
    {
        $("#Process").hide();
        $("#configuration").show();
    } else {
        $("#configuration").hide();
        $("#Process").show();
    }
}
